/**
 * 
 */
package com.yi.backend.ui.config.api.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.util.StringUtils;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.yi.backend.db.api.IMongoDBHelperApi;
import com.yi.backend.db.api.impl.MongoDBConstants;
import com.yi.backend.model.api.impl.MongoCommonModelApi;
import com.yi.backend.ui.config.ConfigTypes;
import com.yi.backend.ui.config.DetailTypes;
import com.yi.backend.ui.config.FieldConfig;
import com.yi.backend.ui.config.GridTypes;
import com.yi.backend.ui.config.SearchTypes;
import com.yi.backend.ui.config.api.IUIConfigApi;

/**
 * @author long
 *
 */
public class MongoUIConfigApi extends MongoCommonModelApi implements IUIConfigApi{
	private IMongoDBHelperApi mongoDBHelperApi;
	public IMongoDBHelperApi getMongoDBHelperApi() {
		return mongoDBHelperApi;
	}

	public void setMongoDBHelperApi(IMongoDBHelperApi mongoDBHelperApi) {
		this.mongoDBHelperApi = mongoDBHelperApi;
	}
	
	private DBObject convertFieldConfigToEntity(FieldConfig fieldConfig){
		DBObject entity = convertCommonModelToEntity(fieldConfig);
		if(StringUtils.hasLength(fieldConfig.objectName)) entity.put("objectName", fieldConfig.objectName);
		entity.put("configType", fieldConfig.configType);
		entity.put("configValue", fieldConfig.configValue);
		return entity;
	}
	@Override
	public String create(FieldConfig fieldConfig) {
		// TODO Auto-generated method stub
		return mongoDBHelperApi.create(schemaName, convertFieldConfigToEntity(fieldConfig));
	}

	@Override
	public void initialBasicUIConfigs() {
		// TODO Auto-generated method stub
		FieldConfig idTblConfig = new FieldConfig("idTblConfig","ui table config","*", ConfigTypes.gridField, GridTypes.checkbox);
		create(idTblConfig);
		
		FieldConfig nameTblConfig = new FieldConfig("nameTblConfig","ui table config","*", ConfigTypes.gridField,GridTypes.text);
		create(nameTblConfig);
		FieldConfig nameSearchConfig = new FieldConfig("nameDetailConfig","ui table config","*", ConfigTypes.searchField,SearchTypes.text);
		create(nameSearchConfig);
		FieldConfig nameDetailConfig = new FieldConfig("nameDetailConfig","ui table config","*", ConfigTypes.detailField,DetailTypes.text);
		create(nameDetailConfig);
		
		FieldConfig descTblConfig = new FieldConfig("descTblConfig","ui table config","*", ConfigTypes.gridField,GridTypes.text);
		create(descTblConfig);
		FieldConfig descDetailConfig = new FieldConfig("descDetailConfig","ui table config","*", ConfigTypes.detailField,DetailTypes.text);
		create(descDetailConfig);
		
		FieldConfig updatedTblConfig = new FieldConfig("updatedTblConfig","ui table config","*", ConfigTypes.gridField,GridTypes.text);
		create(updatedTblConfig);
	}

}
